#include "compile_run.hpp"
#include "../Comm/httplib.h"
#include <iostream>
using namespace httplib;
using namespace ns_compile_and_run;

void Usage(const std::string &command)
{
    std::cout << "Usage : " << command << " port\n";
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        return 1;
    }
    Server ser;
    ser.Get("/hello", [](const Request &req, Response &resp){
        resp.set_content("hello http,你好", "text/plain;charset=utf-8"); 
    });
    ser.Post("/compile_and_run", [](const Request &req, Response &resp){
        std::cout << "收到消息" << std::endl;
        std::string in_json = req.body;
        std::string out_json;
        if(!in_json.empty()){
            CompileAndRun::Start(in_json,&out_json);
            resp.set_content(out_json,"application/json;charset=utf-8");
        } 
    });
    ser.listen("0.0.0.0", atoi(argv[1]));

    // std::string file = "code";
    // if(Compiler::Compile(file))
    //     Runner::Run(file);
    // Json::Value in_json;
    // in_json["code"] = R"(#include <iostream>
    // int main()
    // {
    //     std::cout<<"hello zxws"<<std::endl;
    //     aaaaa
    //     return 0;
    // }
    // )";// raw string
    // in_json["input"] = "";
    // in_json["cpu_limit"] = 1;
    // in_json["memo_limit"] = 10240*5;
    // Json::StyledWriter writer;
    // std::string ret = writer.write(in_json);
    // std::string out;
    // CompileAndRun::Start(ret, &out);
    // std::cout << out << std::endl;
    return 0;
}